<template>
	<view class="addpage">
		<view class="bannerbox">
			<view class="line">
				<view class="left">
					标签
				</view>
				<view class="right">
					<picker class="picker" mode="selector" :value="value"  :range="evenlist" range-key="label"   @change="selectbiaoqian">
							<view class="uni-input" >{{biaoqian}}</view>
						</picker>
					<view class="iconfont icon-xiangyou1">
						
					</view>
				</view>
			</view>
		</view>
		<view class="topbox">
			<textarea  class="put" v-model="neirong" cols="30" placeholder="点击输入" rows="10"></textarea>
		</view>
		<view class="bannerbox">
			<view class="line">
				<view class="left">
					时间
				</view>
				<view class="right">
					<picker class="picker" mode="time" :value="time" start="00:01" end="23:59" @change="bindTimeChange">
							<view class="uni-input">{{time}}</view>
						</picker>
					<view class="iconfont icon-xiangyou1">
						
					</view>
				</view>
			</view>

			<view class="line zhongjian">
				<view class="left">
					重复
				</view>
				
				<view class="right">
					<picker class="picker" mode="selector" :value="value"  :range="array" range-key="label" @change="selectchongfu">
							<view class="uni-input">{{chongfu}}</view>
						</picker>
					<view class="iconfont icon-xiangyou1">
						
					</view>
				</view>
			</view>
			<view class="line zhongjian" v-if="week.length>0">
				<view class="left">
					星期
				</view>
				
				<view class="right" @click="winshow=true" >
				<view class="week"  v-for="(item,index) in week" :key="index">{{item}}</view>
					
				</view>
			</view>

	
		</view>
		<view class="btn" @click="save">
			保存
		</view>
		
		
		<view class="winpage" v-if="winshow">
			<view class="win">
				<view class="wintitle">
					重复提醒周期
					<view class="iconfont icon-guanbi" @click="winshow=false">
						
					</view>
				</view>
				<view class="line">
					<view class="item" :class="isSelected(item) ? 'act' : ''" v-for="(item,index) in weeklist" :key="index" @click="selectweek(item)">
						{{item}}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {addEvent,getDictData} from '../../utils/api.js'
	export default {
		data() {
			return {
				neirong:'',
				day:'',
				time:'',
				chongfu:'',
				biaoqian:'',
				index:'',
				array:[],
				evenlist:[],
				erType:'',
				erLabel:'',
				week:[],
				weeklist:['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
				daylist:[],
				winshow:false
				
				
			}
		},
		onLoad() {
		
	
		},
		onShow() {
			let p={
				type:'sys_shijiantixingbiaoqian'
			}
			getDictData(p).then(res=>{
				console.log(res,'查询字典事件标签')
				this.evenlist=res.data.data.map(item => {
				return {
				    value: item.dictValue,
				   label: item.dictLabel
				   }
			})
			})
			let a={
				type:'sys_chongfuleixing'
			}
			getDictData(a).then(res=>{
				console.log(res,'查询字典重复类型标签')
				this.array=res.data.data.map(item => {
                return {
                    value: item.dictValue,
                    label: item.dictLabel
                     }
                })
			})
		},
		methods: {
			 isSelected(item) {
			      return this.week.some(i => i == item);
			    },
			//选择标签
			selectbiaoqian(e){
				this.erLabel=this.evenlist[e.detail.value].value
				this.biaoqian=this.evenlist[e.detail.value].label
			},
			
			//选择重复
			selectchongfu(e){
					this.erType=this.array[e.detail.value].value
				this.chongfu=this.array[e.detail.value].label
				if(this.array[e.detail.value].value==1){
					this.winshow=true
				}
			},
			//选择时间
			bindTimeChange(e){
				
				this.time=e.detail.value
			},
			//选择周几
			selectweek(item){
				console.log(item)
				 const index = this.week.findIndex(i => i == item);
				 console.log(index)
				      if (index === -1) {
				        // 不在列表2中 → 添加
				        this.week.push(item);
						console.log(this.week)
				      } else {
				        // 已存在 → 删除
				        this.week.splice(index, 1);
						console.log(this.week)
				      }
				    
			},
			//选择日期
			selectday(e){
				this.day=e.detail.value
			},
			save(){
				if(this.erLabel==''){
					uni.showToast({
						title: '请选择标签',
						icon:'none',
						duration: 2000
					});
                  return
				}
				if(this.erTime==''){
					uni.showToast({
						title: '请选择时间',
						icon:'none',
						duration: 2000
					});
				  return
				}
				if(this.erType==''){
					uni.showToast({
						title: '请选择重复',
						icon:'none',
						duration: 2000
					});
				  return
				}
				if(this.neirong==''){
					uni.showToast({
						title: '请输入事件',
						icon:'none',
						duration: 2000
					});
				  return
				}
				if(this.erType==1&&this.week.length==0){
					uni.showToast({
						title: '请选择重复周期',
						icon:'none',
						duration: 2000
					});
				  return
				}
				let p={
					phone:uni.getStorageSync('phone'),
					erLabel:this.erLabel,
					erContent:this.neirong,
					erType:this.erType,
					erTime:this.time+':00',
					weeks:this.week.join()
				}
				addEvent(p).then(res=>{
					console.log(res)
					if(res.data.code==200){
						uni.showToast({
							title: '添加成功',
							icon:'none',
							duration: 1000
						});
						setTimeout(()=>{
							uni.navigateBack()
						},1000)
						
					}else{
						uni.showToast({
							title: res.data.msg,
							icon:'none',
							duration: 2000
						});
					}
				})
			},
		}
	}
</script>

<style lang="less" scoped >
 .addpage{
	 width: 750rpx;
	 background: linear-gradient(180deg, rgba(242,248,255,1) 0%,rgba(255,255,255,0) 76%);
	 box-sizing: border-box;
	 padding: 30rpx;
	 height: 100vh;
	 
 }
 .topbox{
	 width: 100%;
	 height: 400rpx;
	 background-color: white;
 }
 .put{
	 width: 100%;
	 height: 100%;
	 border-radius: 30rpx;
	 padding: 10rpx;
 }
 .bannerbox{
	 width: 100%;
	 background-color: white;
	 box-sizing: border-box;
	 margin-top: 30rpx;
	 border-radius: 20rpx;
 }
 .line{
	 width: 100%;
	 display: flex;
	 align-items: center;
	 justify-content: space-between;
	 padding: 0 30rpx;
	 height: 120rpx;
	 line-height: 120rpx;
	 font-weight: bold;
	 box-sizing: border-box;
	 font-size: 30rpx;
 }
 .left{
	 min-height: 65rpx;
 }
 .week{
	 
 }
 .uni-input{
	 width: 200rpx;
	 height: 120rpx;
	 text-align: center;
 }
 .picker{
	 width: 200rpx;
	 height: 120rpx;
	 line-height: 120rpx;
	 
 }
 .zhongjian{
	 border-top: 1rpx solid #f1f1f1;
	 border-bottom: 1rpx solid #f1f1f1;
 }
 .right{
	 display: flex;
	 align-items: center;
	 color: #B1B1B1 ;
	
	 
 }
 .btn{
	 width: 80%;
	 margin: auto;
	 height: 100rpx;
	 border-radius: 30rpx;
	 text-align: center;
	 color: white;
    line-height: 100rpx;
		 background-color: #1A4F8A;
		 margin-top: 100rpx;
		 font-weight: bold;
 }
 .winpage{
	 width: 750rpx;
	 height: 100vh;
	 z-index: 99999;
	 background-color: rgba(0, 0, 0, 0.5);
	 position: fixed;
	 top: 0;
	 left: 0;
	 bottom: 0;
	 right: 0;
	 .win{
	 	  			 width: 100%;
	 	  			 height: 40%;
	 	  			 position: absolute;
	 	  			 top: 60vh;
	 	  			 bottom: 0;
	 	  			 left: 0;
	 	  			 right: 0;
	 	  			 background-color: #f1f1f1;
	 	  			 border-top-right-radius: 30rpx;
	 	  			 border-top-left-radius: 30rpx;
	 	  			 padding-top: 40rpx;
	 	  			 .wintitle{
	 	  				 width: 100%;
	 	  				 border-top: 1px solid #f1f1f1;
	 	  				 text-align: center;
	 	  				 position: relative;
	 	  				 padding-top: 10rpx;
	 	  				 .iconfont{
	 	  					 position: absolute;
	 	  					 top: 0;
	 	  					 right: 10rpx;
	 	  				 }
	 	  			 }
					 .line{
						 width: 100%;
						 .item{
							 font-size: 28rpx;
							 border-radius: 20rpx;
							 background-color: #B1B1B1;
							 padding: 4rpx 8rpx;
							 height: 50rpx;
							 line-height: 50rpx;
						 }
						 .act{
							 background-color: #1A4F8A !important;
							 color: white !important;
						 }
					 }
	 	  	
	 	  			
	 
	 }
 }
 
</style>
